package persistency;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBLink {
	
	//try{
	
	private Connection connection = null;
	private String url = null;
	private String username = null;
	private String pasw = null;
	private Statement statement = null;
	private ResultSet resultSet = null;
	private boolean connectieMetDB;
	
	public DBLink(String url, String userName, String paswoord)
	{
		this.pasw=paswoord;
		this.url=url;
		this.username=userName;
		this.connection= null;
		this.connectieMetDB = false;
		
	}
	
	//bovenstaande info halen we dan uit de properties file (setup.ini)
	//bij de initialisatie van connectie
	
	public Statement getStatement() {
		return statement;
	}

	public void setStatement(Statement statement) {
		this.statement = statement;
	}

	public ResultSet getResultSet() {
		return resultSet;
	}

	public void setResultSet(ResultSet resultSet) {
		this.resultSet = resultSet;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPasw() {
		return pasw;
	}

	public void setPasw(String pasw) {
		this.pasw = pasw;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
	
	public Connection getConnection() throws SQLException {
		if (this.connection == null) {
			this.connection = DriverManager.getConnection(url, username, pasw );
		}
		return connection;
	}
	
	public void controleerConnectie(Connection connection)
	{
		if(this.connection != null)
		{
		this.connectieMetDB = true;
		}
		else
		{
			throw new IllegalStateException("Not connected to BD");
		}
	}
	
	public void setConnection(Connection connection) {
		this.connection = connection;
	}
	
	public void disconnectFromDB()
	{
		if(connectieMetDB==true)
		{
			try
			{
				this.resultSet.close();
				this.statement.close();
				this.connection.close();
			}catch(SQLException sqlException)
			{
				sqlException.printStackTrace();
			}
			finally
			{
				this.connectieMetDB = false;
			}
		}
	}	
}
